package com.atcjx.MyUtils;


import com.atcjx.pojo.Article.ArticleDTO;
import com.atcjx.pojo.Article.baseArticle;
import com.atcjx.pojo.keyWord;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName wordSegUtils.java
 * @Description TODO
 * @createTime 2021年12月06日 21:25:00
 */
public class wordSegUtils {
    /**
     * @return java.util.List<java.lang.String>
     * @Author Cjx_71023
     * @Description //TODO 将传入的text使用IK分词
     * @Date 21:27 2021/12/6
     * @Param [text]
     **/
    public static List<keyWord> segment(String text,int priority) throws IOException {
        List<keyWord> list = new ArrayList<>();
        StringReader re = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(re, true);
        Lexeme lex;
        while ((lex = ik.next()) != null) {
            String str=lex.getLexemeText();
            if(str.length()>25) {
                continue;
            }
            list.add(new keyWord(str,priority));
        }
        return list;
    }
    public static List<String> segment(String text) throws IOException {
        List<String> list = new ArrayList<>();
        StringReader re = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(re, true);
        Lexeme lex;
        while ((lex = ik.next()) != null) {
            list.add(lex.getLexemeText());
        }
        return list;
    }

    /**
     * @return java.util.List<java.lang.String>
     * @Author Cjx_71023
     * @Description //TODO 将传入的ArticleDto分词
     * @Date 21:37 2021/12/6
     * @Param [article]
     **/
    public static List<keyWord> segmentArticle(baseArticle article) {
        Set<keyWord> set = new HashSet<>();
        /*
        * 顺序不能颠倒
        * */
        try {
            set.addAll(segment(article.getTitle(),25));
            set.addAll(segment(article.getCategory(),20));
            set.addAll(segment(article.getTheme(),15));
            set.addAll(segment(article.getAuthor(),10));
            set.addAll(segment(article.getContent(),1));
        } catch (Exception e) {
            System.out.println("ok");
        }
        return new ArrayList<keyWord>(set);
    }
}
